package com.mfs.proxy.core.server.endpoint;

import com.mfs.common.constant.Constant;
import com.mfs.proxy.core.constant.AppConstant;
import lombok.Data;

import java.io.IOException;
import java.nio.channels.SelectionKey;

/**
 * @author mfs
 * @date 2023/7/2 12:30
 **/
@Data
public abstract class AbstractProxyEndpoint implements ProxyEndpoint{
    /**
     * 名称
     * */
    protected String name;
    /**
     * 代理IP
     * */
    protected String proxyIp;
    /**
     * 代理端口
     * */
    protected Integer proxyPort;
    /**
     * 代理 selectionKey
     * */
    protected SelectionKey proxySelectionKey;

    public AbstractProxyEndpoint(String proxyIp, Integer proxyPort, SelectionKey proxySelectionKey) {
        this.name = AppConstant.PROXY_ENDPOINT + Constant.MIDDLE_LINE + proxyIp + proxyPort;
        this.proxyIp = proxyIp;
        this.proxyPort = proxyPort;
        this.proxySelectionKey = proxySelectionKey;
    }

    @Override
    public void processProxyRequest(SelectionKey selectionKey) {

    }

    @Override
    public void close() throws IOException {
        proxySelectionKey.channel().close();
        proxySelectionKey.cancel();
    }
}
